#!/usr/bin/env python
# encoding: utf-8
"""
#-------------------------------------------------------------------#
#                   CONFIDENTIAL --- CUSTOM STUDIOS                 #     
#-------------------------------------------------------------------#
#                                                                   #
#                   @Project Name : Globallawonline                #
#                                                                   #
#                   @File Name    : khm_cons_case.py                      #
#                                                                   #
#                   @Programmer   : 李建                            #
#                                                                   #  
#                   @Start Date   : 2021/7/13 14:49                 #
#                                                                   #
#                   @Last Update  : 2021/7/13 14:49                 #
#                                                                   #
#-------------------------------------------------------------------#
# Classes:柬埔寨宪法委员会网站判例                                                          #
#                                                                   #
#-------------------------------------------------------------------#
"""
import hashlib

import scrapy

from GlobalLawCaseSpider.items import MyFileItem


class Spider(scrapy.Spider):
    name = 'khm_cons_case'
    allowed_domains = ['ccc.gov.kh']
    # 柬埔寨宪法委员会网站地址
    start_urls = ['https://ccc.gov.kh/index.php']

    # 解析初始页
    def parse(self, response):
        # 获得每页列表
        trsl = response.xpath('/html/body/header/nav/ul/li[6]/ul/div/li')
        for trs in trsl:
            tr = trs.xpath('./a')
            # 模块网站
            modelUrlS = tr.xpath('./@href')
            if len(modelUrlS):
                modela = tr[0]
                yield response.follow(modela, callback=self.modelparse, dont_filter=True)

    # 解析年份页面
    def modelparse(self, response):
        # 获得年份网址成员列表
        trs = response.xpath('//*[@id="middle"]/div[2]/ul/li/article/table//tr[1]/td//a')     # //*[@id="middle"]/div[2]/ul/li//table/tbody/tr[1]/td//a
        for tr in trs:
            detailUrlS = tr.xpath('./@href')
            if len(detailUrlS):
                detaila = tr
                yield response.follow(detaila, callback=self.detailed, dont_filter=True)

    # 解析详情页
    def detailed(self, response):
        item = MyFileItem()
        if response.status == 200:
            # 法律详细网址
            detailUrl = response.url
            # 判例标题
            titleS = response.xpath('//*[@id="middle"]/div/p/text()')
            title = ''
            if len(titleS):
                for titleSl in titleS:
                    title = title + ' ' + titleSl.get().strip()
                title = title.strip()  # 去除左右两边的空格

            # 裁决日期
            adjudicationDateS = response.xpath('//*[@id="middle"]/div/span/text()')
            adjudicationDate = ''
            if len(adjudicationDateS):
                for adjudicationDateSl in adjudicationDateS:
                    adjudicationDate = adjudicationDate + ' ' + adjudicationDateSl.get().strip()
                adjudicationDate = adjudicationDate.strip()  # 去除左右两边的空格
            # 下载地址
            downloadUrlS = response.xpath('//*[@id="middle"]/div/a/@href')
            downloadUrl = ''
            if len(downloadUrlS):
                downloadUrl = downloadUrlS[0].get().strip()
                downloadUrl = 'https://' + self.allowed_domains[0] + '/' + downloadUrl

            # 国别及文件信息
            country = 'Cambodia'
            website = 'constitution'
            modular = 'case'
            # 下载文件名
            fina = ''
            if len(detailUrl) > 0:
                fina = 'f' + str(hashlib.md5(detailUrl.encode('utf-8')).hexdigest())

            # 唯一ID
            systemid = str(hashlib.md5(detailUrl.encode('utf-8')).hexdigest())


            if len(downloadUrl):
                item['file_urls'] = ''
                item['country'] = country
                item['website'] = website
                item['modular'] = modular
                item['ext'] = 'pdf'
                item['fina'] = fina
                item['title'] = title
                item['abstractUrl'] = ''
                item['abstract'] = ''
                item['detail'] = ''
                item['detailUrl'] = ''
                item['downloadUrl'] = downloadUrl

                item['Title'] = title
                item['CaseNumber'] = ''
                item['KeyWord'] = ''
                item['SortA'] = 'LAWCOUNTRYJPZ'
                item['People'] = ''
                item['CaseOfAction'] = ''
                item['UseLaw'] = ''
                item['AdjudicationDate'] = adjudicationDate
                item['FullText'] = ''
                item['JudgAgency'] = ''
                item['SortB'] = 'LANGUAGEGMY'
                item['SortC'] = ''
                item['CaseSummary'] = ''
                item['Articles'] = ''
                item['Chapter'] = ''
                item['Section'] = ''
                item['SYS_FLD_DIGITFILENAME'] = fina
                item['FileUrl'] = ''
                item['AbstractFileName'] = ''
                item['DownLoadUrl'] = detailUrl
                item['DownLoadWebNameC'] = '柬埔寨宪法委员会'
                item['DownLoadWebNameE'] = "Cambodian Constitutional Council"
                item['SYSID'] = systemid
                item['Website'] = 'Constitutional precedent'
                item['Isconversion'] = '0'
                item['CaseDate'] = ''

                yield item




